package by.yakusekmaxim.bookgid.webapp.page.admin.book;

import javax.inject.Inject;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Book;
import by.yakusekmaxim.bookgid.datamodel.Book_;
import by.yakusekmaxim.bookgid.datamodel.enums.PublicationStatus;
import by.yakusekmaxim.bookgid.services.BookService;
import by.yakusekmaxim.bookgid.webapp.page.AdminLayout;

public class BookPage extends AdminLayout {

	@Inject
	BookService bookService;

	private Filter<Book> filter = new Filter<Book>();

	protected void onInitialize() {
		super.onInitialize();
		
		add(new Link<Object>("linkEditBookPage") {
			@Override
			public void onClick() {
				setResponsePage(new EditBookPage(new Book()));
			}
		});

		Link<Object> countAll = (new Link<Object>("linkCountAll") {

			@Override
			public void onClick() {
				BookPage bookPage = new BookPage();
				setResponsePage(bookPage);
			}
		});
		countAll.setEnabled(bookService.getCount() != 0);
		countAll.add(new Label("countAll", bookService.getCount()));
		add(countAll);

		Link<Object> countEdited = (new Link<Object>("linkCountEdited") {

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(Book_.status);
				filter.setValue(PublicationStatus.publicationstatus_edited);
				BookPage bookPage = new BookPage(filter, new ResourceModel(
						"p.admin.book.pageName.allEdited").getObject());
				setResponsePage(bookPage);
			}
		});
		countEdited.setEnabled(bookService.getCount(
				Book_.status, PublicationStatus.publicationstatus_edited) != 0);
		countEdited.add(new Label("countEdited", bookService.getCount(
				Book_.status, PublicationStatus.publicationstatus_edited)));
		add(countEdited);

		Link<Object> countValidation = (new Link<Object>("linkCountValidation") {

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(Book_.status);
				filter.setValue(PublicationStatus.publicationstatus_validation);
				BookPage bookPage = new BookPage(filter, new ResourceModel(
						"p.admin.book.pageName.allValidation").getObject());
				setResponsePage(bookPage);
			}
		});
		countValidation.setEnabled(bookService.getCount(
				Book_.status, PublicationStatus.publicationstatus_validation) != 0);
		countValidation.add(new Label("countValidation", bookService.getCount(
				Book_.status, PublicationStatus.publicationstatus_validation)));
		add(countValidation);

		Link<Object> countPublished = (new Link<Object>("linkCountPublished") {

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(Book_.status);
				filter.setValue(PublicationStatus.publicationstatus_published);
				BookPage bookPage = new BookPage(filter, new ResourceModel(
						"p.admin.book.pageName.allPublished").getObject());
				setResponsePage(bookPage);
			}
		});
		countPublished.setEnabled(bookService.getCount(
				Book_.status, PublicationStatus.publicationstatus_published) != 0);
		countPublished.add(new Label("countPublished", bookService.getCount(
				Book_.status, PublicationStatus.publicationstatus_published)));
		add(countPublished);
	}

	public BookPage(Filter<Book> filter, String string) {
		add(new Label("pageNameLabel", string));
		add(new BookListPanel("bookList", filter, this.getPage()));
		
	}

	public BookPage() {
		filter.setEnabled(false);
		add(new Label("pageNameLabel", getPageTitle()));
		add(new BookListPanel("bookList",filter, this.getPage()));
	}

	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel("adminmenu.books");
	}

	@Override
	protected WebMarkupContainer getTarget() {
		return targetBook;
	}

}
